﻿/*
* User.cpp
*
* 用户信息
*
* create date: 2014.4.11
* create by: leo
*
*/

#include "User.h"

#include "../heros/HeroBuilder.h"

USING_NS_CC;

CUser& CUser::getInstance()
{
	static CUser user;
	return user;
}

void CUser::login(cocos2d::CCObject* callbackObj, cocos2d::SEL_CallFunc loginSuccess)
{
	if (isLogin()) return;

	mCallbackObj = callbackObj;
	mOnLoginSuccess = loginSuccess;
	if (NULL == mCallbackObj || NULL == mOnLoginSuccess)
	{
		mCallbackObj = this;
		mOnLoginSuccess = callfunc_selector(CUser::doNothing);
	}


	if (NULL == mCorps)
	{
		mCorps = new CBaseCorps();
	}

	mCorps->removeAllHero();

	mCorps->addHero(*(CHeroBuilder::getInstance().createHero("guanyu")));

    CBaseHero* hero = CHeroBuilder::getInstance().createHero("zhugeliang");
    hero->setCorpsPos(CBaseCorps::SECOND);
	mCorps->addHero(*hero);

	hero = CHeroBuilder::getInstance().createHero("guanyu");
    hero->setCorpsPos(CBaseCorps::THIRD);
	mCorps->addHero(*hero);

	hero = CHeroBuilder::getInstance().createHero("zhugeliang");
    hero->setCorpsPos(CBaseCorps::FOURTH);
	mCorps->addHero(*hero);

    (mCallbackObj->*mOnLoginSuccess)();

}

void CUser::logout()
{
	CC_SAFE_DELETE(mCorps);
}

bool CUser::isLogin()
{
	return mLoggedIn;
}

void CUser::prepareEnterBattle(CBattleScene& scene)
{
	mCorps->preloadRes();
	scene.setLeftCorps(*mCorps);
}

CUser::CUser()
: mCorps(NULL),
  mLoggedIn(false),
  mCallbackObj(this),
  mOnLoginSuccess(callfunc_selector(CUser::doNothing)),
  mID(0),
  mLevel(0),
  mExp(0)
{}

CUser::~CUser()
{}

void CUser::doNothing()
{}
